package com.ivo.web.form.def;

import java.util.ArrayList;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.ivo.util.ListMap;
import com.ivo.util.StringUtil;
import com.ivo.web.form.intf.Component;
import com.ivo.web.form.intf.Layout;

public class Form extends ComponentImpl
{
	private String action;
	private String method;
	private Layout layout;
	private List<Component> fields;
	private ListMap<String, Component> items;

	public Form()
	{
		this.fields = new ArrayList<Component>();
		this.items = new ListMap<String, Component>();
	}

	public String getAction()
	{
		return action;
	}

	public void setAction(String action)
	{
		this.action = action;
	}

	public String getMethod()
	{
		return method;
	}

	public void setMethod(String method)
	{
		this.method = method;
	}

	public Layout getLayout()
	{
		return layout;
	}

	public void setLayout(Layout layout)
	{
		this.layout = layout;
	}

	public List<Component> getFields()
	{
		return fields;
	}

	public void setFields(List<Component> fields)
	{
		this.fields = fields;
	}

	public ListMap<String, Component> getItems()
	{
		return items;
	}

	public void setItems(ListMap<String, Component> items)
	{
		this.items = items;
	}

	@Override
	public String toHtml()
	{
		Document document = DocumentHelper.createDocument();
		Element element = document.addElement( "form" );
		//-------------------------------------------------------------------------------------
		if( StringUtil.notEmpty( id ) ) element.addAttribute( "id", id );
		if( StringUtil.notEmpty( name ) ) element.addAttribute( "name", name );
		if( StringUtil.notEmpty( style ) ) element.addAttribute( "style", style );
		if( StringUtil.notEmpty( css ) ) element.addAttribute( "css", css );
		if( StringUtil.notEmpty( action ) ) element.addAttribute( "action", action );
		if( StringUtil.notEmpty( method ) ) element.addAttribute( "method", method );
		//-------------------------------------------------------------------------------------
		return element.asXML();
	}
}
